package xyz.wpxy.wxpushmsg.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.wpxy.wxpushmsg.common.R;

/**
 * @Description: 统一异常处理类
 * @author: Peng Wei
 * @date:2023/8/7 16:30
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理
     */
    @ExceptionHandler(Exception.class)
    public R error(Exception e) {
        //控制台打印:
        log.warn("{}", e.getMessage());
        return R.error().message("服务器正忙");
    }

    /**
     * 算数异常捕获
     */
    @ExceptionHandler(ArithmeticException.class)
    public R arithmeticExceptionHandler(ArithmeticException e) {
        log.warn("{}", e.getMessage());
        return R.error().message("算术异常！" + e.getMessage());
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(ServiceException.class)
    public R serviceExceptionHandler(ServiceException e) {
        log.warn("{}", e.getMessage());
        return R.error().message(e.getMessage());
    }


}
